/***
*	For this project,  we must write some code for our medical prescription system
*	where it includes input code from user.
*
*/

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fcntl.h>

using namespace std;

void runDoctorLogin();
void runUserPasswordChange();
bool patientExists (const string file_name);

enum error_type {INVALID_INPUT, WRONG_DATA, PATIENT_NOT_EXITS, TEST_TYPE };
error_type errors_name;

const string error_msg = "Invalid Input";

const size_t MAX_INPUT_STRING_SIZE = 15;

int main()
{
    srand(time(NULL));

	runDoctorLogin();

	runUserPasswordChange();

	const string patient_file_name = "ssw556_patient_file.txt";

	if (patientExists(patient_file_name)) {

	} else {
		errors_name = PATIENT_NOT_EXITS;
		cout << "Error (type " << errors_name << "): " << error_msg << endl;
	}



	return 0;
}


/***
*	In this function, the doctor is prompted to enter a user name and password.
*	Iff he enters "valid" user name and password strings, the client attempts
*	to log in with server.  If he enters invalid pairs, the function returns.
*
*/
void runDoctorLogin()
{
	string input;
	string UserId;
	string Password;

	unsigned int maxPasswordTries = 3;

	cout << "Enter doctor id:" << endl;
	getline(cin, input);

	if ( input.size() > MAX_INPUT_STRING_SIZE || input.size() <= 0 )
	{
		cout << "Invalid user name given";
		return;
	}
	else
	{
		UserId = input;
	}

	unsigned int numTries = 0;
	bool passwordValid = false;

	while ( numTries < maxPasswordTries && !passwordValid )
	{
		cout << "Enter password:" << endl;
		getline(cin, input);

		if ( input.size() > MAX_INPUT_STRING_SIZE || input.size() <= 0 )
		{
			cout << "Invalid password" << endl;
			numTries++;
		}
		else
		{
			Password = input;
			passwordValid = true;
		}
	}

	if ( !passwordValid ) 
	{
		return;
	}
	else
	{
		cout << "Attempting to log in to server with username = " <<  UserId 
			<< " and password = " << Password  << endl;
	}

}
bool patientExists (const string file_name) {
   int fd;
   fd = open (file_name.c_str(), O_WRONLY | O_CREAT | O_EXCL,S_IRWXU);

   if (fd == -1) {
	  errors_name = INVALID_INPUT;
      cout << "Error (type " << errors_name << "): " << error_msg << endl;
      return false;
   } else {
	  return true;
   }
}

void runUserPasswordChange()
{
    const size_t tokenLength = 16;
    const string chars = "abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*";
    const size_t sz = chars.size();

    cout << endl << endl << "Password reset" << endl;
    cout << "==============" << endl;

    /* Generate a one-time use token. This would normally be stored in the
     * database and emailed to the user, as either a raw string or in a URL
     * query string (i.e. www.myapp.com/reset?token=xxx).
     */
    string token;
    while (token.size() <= tokenLength) token.push_back(chars[rand() % sz]);
    cout << "Your password reset code is " << token << endl;

    /* Again, this would be user initiated, and the one-time use code would
     * be pulled from the database.
     */
    string userToken;
    cout << "Enter code to reset password: ";
    getline(cin, userToken);
    if (token.compare(userToken))
    {
        cout << "Incorrect code entered. Password reset failed." << endl << endl;
        return;
    }

    /* If the token is correct, enter a new password. */
    string pass1, pass2;
    bool match = false;
    while (!match)
    {
        cout << "Enter new password: ";
        getline(cin, pass1);
        cout << "Repeat new password: ";
        getline(cin, pass2);

        if (!pass1.compare(pass2)) match = true;

        /* The password would also need to pass minimum security reqs. */
    }

    /* Remove the one-time use token from the database. */
    token.clear();
    userToken.clear();

    cout << "Password successfully reset." << endl << endl;
}
